package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    public static final Short PASS_AUTH = 9; //审核通过
    public static final Short FAIL_AUTH = 2; //审核失败
    public static final Integer WM_USER =2; //自媒体平台用户

    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private ApUserMapper apUserMapper;


    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {

        //参数为空
        if (dto == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        dto.checkParam();

        QueryWrapper<ApUserRealname> queryWrapper =new QueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getStatus().toString())){
            queryWrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
        }

        Page pageparam = new Page(dto.getPage(), dto.getSize());
        IPage page =page(pageparam, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setCode(0);
        responseResult.setData(page.getRecords());
        return responseResult;
    }


    /**
     * 普通用户审核成为自媒体人
     *
     * @param dto
     * @return
     */
    @Override
    @GlobalTransactional  //seata 全局分布式事务统一管理
    public ResponseResult updateStatusById(AuthDto dto) {

        //1.检查参数
        if (dto == null || dto.getId()==null ||dto.getStatus() ==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:跟新 ap_user_realname  状态改为
        ApUserRealname apUserRealname= new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if(dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //3: status 9   feign wemedia  添加一条
        if(dto.getStatus().equals(PASS_AUTH)){

            //wemedia插入数据
            //参数 id  ap_user_realname   ==》userId --》apUser 主键id --》userName==>wm_user  name
            ApUserRealname userRealname = this.getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            if(apUser ==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            WmUser wmUser = wemediaFeign.findByName(apUser.getName());
            if(wmUser ==null){
                wmUser = new WmUser();
                //设置ApUserId
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(PASS_AUTH.intValue());
                wmUser.setPhone(apUser.getPhone());
                wemediaFeign.save(wmUser);
            }

            //4: status 9    fegtin   article   ap_author  添加数据
            ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
            if(apAuthor==null){
                apAuthor = new ApAuthor();
                apAuthor.setName(wmUser.getName());
                apAuthor.setType(WM_USER); //平台自媒体人
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(wmUser.getApUserId());
                apAuthor.setWmUserId(wmUser.getId());
                articleFeign.save(apAuthor);
            }

            int a=10/0;
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
